package com.greateamd.timetableasy.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

@SuppressWarnings("serial")
@Entity
@Table(name="campuses")
public class Campus extends LightEntity implements PersistableEntity,HasEventsEntity,Serializable
{

	@Id
	@GeneratedValue
	private Integer id;
	private String name;
	private String address;
	
	@Column(name="enabled",nullable=false)
	private boolean enabled = true;

	@ManyToOne
	@JoinColumn(nullable=false,name="university")
	private University university;
	
	@ManyToMany(fetch = FetchType.LAZY,targetEntity=User.class,cascade=CascadeType.ALL)
    @JoinTable(name="campus_manager",
		        joinColumns = @JoinColumn(columnDefinition="campus"),
		        inverseJoinColumns=@JoinColumn(name="manager"))
	private Set<User> managers;
	
	@OneToMany(mappedBy="campus", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
	private Set<Room> rooms;

	@OneToMany(mappedBy="campus", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	private Set<Promotion> promotions;
	
	@ManyToMany(fetch = FetchType.LAZY,targetEntity=Event.class,cascade=CascadeType.ALL)
    @JoinTable(name="campuses_events",
		        joinColumns = @JoinColumn(columnDefinition="campus"),
		        inverseJoinColumns=@JoinColumn(name="event_id"))
	private Set<Event> events;
	
	public Campus()
	{}

	public Campus(String name, String address) 
	{
		this.name = name;
		this.address = address;
	}

	public Integer getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getAddress() {
		return address;
	}

	@Override
	public boolean isEnabled() {
		return enabled;
	}

	public Set<User> getManagers() {
		return managers;
	}

	public University getUniversity() {
		return university;
	}

	public Set<Promotion> getPromotions() {
		return promotions;
	}

	public void setUniversity(University university) {
		this.university = university;
	}

	public void setPromotions(Set<Promotion> promotions) {
		this.promotions = promotions;
	}

	public Set<Room> getRooms() {
		return rooms;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setManagers(Set<User> managers) {
		this.managers = managers;
	}

	public void setRooms(Set<Room> rooms) {
		this.rooms = rooms;
	}
	
	public Set<Event> getEvents() {
		return events;
	}

	public void setEvents(Set<Event> events) {
		this.events = events;
	}

	public void addManager(User user) {
		if (managers == null)
			managers = new HashSet<User>();
		this.managers.add(user);
	}
	
	public void addRoom(Room room) {
		if (rooms == null)
			rooms = new HashSet<Room>();
		this.rooms.add(room);
	}
	
	public void addEvent(Event e) {
		if (events == null)
			events = new HashSet<Event>();
		this.events.add(e);
	}
	
	@Override
	public String toString() {
		return getName();
	}
}
